﻿using Infinity.BusinessLayer;
using Infinity.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using PagedList.Mvc;

namespace Infinity.Areas.Managerial.Controllers
{
    public class CourseController : Controller
    {
        InfinityEntities context = new InfinityEntities();

        public ActionResult Index(int? page)
        {
           
            PagedList.IPagedList<CourseList> list = context.Courses.Select(c => new CourseList
            {
                Course_Name = c.Course_Name,
                CourseID = c.CourseID,
                Exam = c.Exam,
                Official_crs_hrs = c.Official_crs_hrs
            }).ToList().ToPagedList(page ?? 1, 4);
           return View(list);
        }
        [HttpPost]
        public ActionResult Index(string searchBy, string searchfor, int? page)
        {
            PagedList.IPagedList<CourseList> list = context.Courses.Select(c => new CourseList
            {
                Course_Name = c.Course_Name,
                CourseID = c.CourseID,
                Material = context.course_material_certificate.Where(crs => crs.CourseID == crs.CourseID).FirstOrDefault().material,
                Certificate = context.course_material_certificate.Where(crs => crs.CourseID == crs.CourseID).FirstOrDefault().certificate,
                //  Exam = c.Exam,
                Official_crs_hrs = c.Official_crs_hrs
               ,
                CategoryID = c.CategoryID
            }).ToList().ToPagedList(page ?? 1, 4);
            if (searchfor != "")
            {
                if (searchBy == "name")
                {
                    list = list.Where(i => i.Course_Name.ToLower().Contains(searchfor.ToLower()) || searchfor == null).ToList().ToPagedList(page ?? 1, 4);
                }

                if (searchBy == "Official_crs_hrs")
                {
                    list = list.Where(i => i.Official_crs_hrs == Decimal.Parse(searchfor) || searchfor == null).ToList().ToPagedList(page ?? 1, 4);
                }
                if (searchBy == "Exam")
                {
                    list = list.Where(i => i.Exam == bool.Parse(searchfor) || searchfor == null).ToList().ToPagedList(page ?? 1, 4);
                }
                if (searchBy == "Material")
                {
                    list = list.Where(i => i.Material == double.Parse(searchfor) || searchfor == null).ToList().ToPagedList(page ?? 1, 4);
                }
                if (searchBy == "Certificate")
                {
                    list = list.Where(i => i.Certificate == double.Parse(searchfor) || searchfor == null).ToList().ToPagedList(page ?? 1, 4);
                }
            }

            return View(list);
        }
        
        public ActionResult EditeCourse(decimal CrsID)
        {
            Course crs = context.Courses.Where(c => c.CourseID == CrsID).FirstOrDefault();
            course_material_certificate cm = context.course_material_certificate.Where(c => c.CourseID == CrsID).FirstOrDefault();
            List<CourseGroup_typeViewModel> gt = context.course_grouptype.Where(c => c.CourseID == CrsID)
                                                  .Select(c => new CourseGroup_typeViewModel
                                                  {
                                                      GrouptypeID = c.GrouptypeID,
                                                      NationalityID = c.NationalityID,
                                                      Price = c.Price
                                                       
                                                  }).ToList<CourseGroup_typeViewModel>();
            List<OutlineViewModel> olList = context.Outlines.Where(o => o.CourseID == CrsID)
                                                .Select(c => new OutlineViewModel
                                                {
                                                    Outline_Name = c.Outline_Name,
                                                    OutlineID = c.OutlineID
                                                }).ToList<OutlineViewModel>();


            CourseViewModel crse = new CourseViewModel
            {
                CourseID = CrsID,
                Course_Name = crs.Course_Name,
                Official_crs_hrs = crs.Official_crs_hrs,
                Exam=crs.Exam,
                CategoryID = crs.CategoryID,


            };
            if (cm == null)
            {
                crse.Material = 0;
                crse.Certificate = 0;
            }
            else
            {
                crse.Material = cm.material;
                crse.Certificate = cm.certificate;

            }
            if (olList != null)
            {
                crse.Outlines = olList;
            }
            if (gt != null)
            {
                crse.course_grouptype = gt;
            }
            ViewBag.catList = context.Categories.ToList();
            ViewBag.nationality = context.Nationalities.ToList();
            ViewBag.group_type = context.Group_type.ToList();
            return View(crse);
            // return View
        }

        [ValidateAntiForgeryToken]
        public ActionResult UpdateCourse(CourseViewModel crs)
        {
            if (ModelState.IsValid)
            {
                ViewBag.catList = context.Categories.ToList();
                Course c = context.Courses.Where(cr => cr.CourseID == crs.CourseID).FirstOrDefault();
                c.CourseID = crs.CourseID;
                c.Course_Name = crs.Course_Name;
                c.CategoryID = crs.CategoryID;
                c.Official_crs_hrs = crs.Official_crs_hrs;
                c.Exam = crs.Exam;
                context.SaveChanges();

                course_material_certificate CM = context.course_material_certificate.Where(m => m.CourseID == crs.CourseID).FirstOrDefault();
                if (CM != null)
                {
                    CM.material = crs.Material;
                    CM.certificate = crs.Certificate;
                    CM.CourseID = crs.CourseID;
                    context.course_material_certificate.Add(CM);
                    context.SaveChanges();
                }
                else
                {
                    course_material_certificate NewCM = new course_material_certificate();
                    NewCM.material = crs.Material;
                    NewCM.certificate = crs.Certificate;
                    NewCM.CourseID = crs.CourseID;
                    context.course_material_certificate.Add(NewCM);
                    context.SaveChanges();

                }
                List<Outline> listOL = context.Outlines.Where(o => o.CourseID == crs.CourseID).ToList();
                if (listOL != null)
                {
                    listOL = crs.Outlines.Select(l => new Outline
                      {
                          CourseID = l.CourseID,
                          OutlineID = l.OutlineID,
                          Outline_Name = l.Outline_Name

                      }).ToList();
                    context.SaveChanges();
                }
                else
                {
                    List<Outline> listOList = crs.Outlines.Select(l => new Outline
                    {
                        CourseID = l.CourseID,
                        OutlineID = l.OutlineID,
                        Outline_Name = l.Outline_Name

                    }).ToList();

                    foreach (var item in listOList)
                    {
                        context.Outlines.Add(item);
                    }
                    context.SaveChanges();
                }
                List<course_grouptype> listgt = context.course_grouptype.ToList();
                if (listgt != null)
                {
                    listgt = crs.course_grouptype.Select(l => new course_grouptype
                    {
                        CourseID = l.CourseID,
                        GrouptypeID = l.GrouptypeID,
                        NationalityID = l.NationalityID,
                        Price = l.Price
                    }).ToList();
                    context.SaveChanges();//save in db
                }
                else
                {
                    List<course_grouptype> listgtype = crs.course_grouptype.Select(l => new course_grouptype
                    {
                        CourseID = l.CourseID,
                        GrouptypeID = l.GrouptypeID,
                        NationalityID = l.NationalityID,
                        Price = l.Price
                    }).ToList();

                    foreach (var item in listgtype)
                    {
                        context.course_grouptype.Add(item);
                    }
                    context.SaveChanges();//save in db
                }
                return View("editeCourse", crs);
            }
            else
            {

                return View("editeCourse", crs); 
            }
        }
        [HttpPost]
        public ActionResult DeleteCourse(decimal crsID)
        {   //material - certfication
            var x = context.course_material_certificate.Where(c => c.CourseID == crsID).FirstOrDefault();
            if (x != null)
            { context.course_material_certificate.Remove(x); }

            //  course_grouptype
            if (context.course_grouptype.Where(c => c.CourseID == crsID).FirstOrDefault() != null)
            {
                context.course_grouptype.Remove(context.course_grouptype.Where(c => c.CourseID == crsID).FirstOrDefault());
            }
            //outline
            if (context.Outlines.Where(c => c.CourseID == crsID).FirstOrDefault() != null)
            { context.Outlines.Remove(context.Outlines.Where(c => c.CourseID == crsID).FirstOrDefault()); }

            context.SaveChanges();
            context.Courses.Remove(context.Courses.Where(c => c.CourseID == crsID).FirstOrDefault());
            context.SaveChanges();

            List<CourseList> list = context.Courses.Select(c => new CourseList
            {
                Course_Name = c.Course_Name,
                CourseID = c.CourseID,
                Material = context.course_material_certificate.Where(cr => cr.CourseID == cr.CourseID).FirstOrDefault().material,
                Certificate = context.course_material_certificate.Where(cr => cr.CourseID == cr.CourseID).FirstOrDefault().certificate,
                //   Exam=c.Exam,
                Official_crs_hrs = c.Official_crs_hrs
                   ,
                CategoryID = c.CategoryID
            }).OrderByDescending(b => b.Course_Name).ToList();
            return View("_CourseList", list);
        }
        public ActionResult AddNewOutline(int send)
        {
            OutlineViewModel o = new OutlineViewModel();
            o.create();
            return PartialView("_Outline", o.create());
        }
        public ActionResult NewCourse()
        {
            CourseViewModel Crs = new CourseViewModel();

            Crs.CourseID = DateTime.Now.Ticks;
            ViewBag.catList = context.Categories.ToList();
            ViewBag.nationality = context.Nationalities.ToList();
            ViewBag.group_type = context.Group_type.ToList();

            OutlineViewModel outl = new OutlineViewModel { CourseID = Crs.CourseID, Outline_Name = "", OutlineID = DateTime.Now.Ticks };
            Crs.Outlines.Add(outl);
            CourseGroup_typeViewModel gt = new CourseGroup_typeViewModel { CourseID = Crs.CourseID };
            //gt.create();
            Crs.course_grouptype.Add(gt);
            return View(Crs);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult NewCourse(CourseViewModel crs)
        {
            if (ModelState.IsValid)
            {
                Course c = new Course();
                c.CourseID = crs.CourseID;
                c.Course_Name = crs.Course_Name;
                c.CategoryID = crs.CategoryID;
                c.Official_crs_hrs = crs.Official_crs_hrs;
                c.Exam = crs.Exam;
                context.Courses.Add(c);
                context.SaveChanges();
                course_material_certificate CM = new course_material_certificate();
                CM.material = crs.Material;
                CM.certificate = crs.Certificate;
                CM.CourseID = crs.CourseID;
                context.course_material_certificate.Add(CM);
                context.SaveChanges();
                List<Outline> listOL = crs.Outlines.Select(l => new Outline
                {
                    CourseID = l.CourseID,
                    OutlineID = l.OutlineID,
                    Outline_Name = l.Outline_Name

                }).ToList();

                foreach (var item in listOL)
                {
                    context.Outlines.Add(item);
                }
                context.SaveChanges();
                List<course_grouptype> listgt = crs.course_grouptype.Select(l => new course_grouptype
                {
                    CourseID = l.CourseID,
                    GrouptypeID = l.GrouptypeID,
                    NationalityID = l.NationalityID,
                    Price = l.Price
                }).ToList();

                foreach (var item in listgt)
                {
                    context.course_grouptype.Add(item);
                }
                context.SaveChanges();//save in db
                return RedirectToAction("Index");
            }
            else return View(crs);
        }

    }
}
